home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / port2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  3.3 KB  |  82 lines

  1. ;/* port2.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 port2.c
  3. Blink FROM LIB:c.o,port2.o TO port2 LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. ** port2.c - port and message example, run at the same time as port1.c
  7. */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/ports.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/alib_protos.h>
  15. #include <stdio.h>
  16.  
  17. #ifdef LATTICE
  18. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL-C handling */
  19. int chkabort(void) {return(0);}
  20. #endif
  21.  
  22. BOOL SafePutToPort(struct Message *, STRPTR);
  23.  
  24. struct XYMessage {
  25.     struct Message xy_Msg;
  26.     WORD           xy_X;
  27.     WORD           xy_Y;
  28. };
  29.  
  30. void main(int argc, char **argv)
  31. {
  32.     struct MsgPort *xyreplyport;
  33.     struct XYMessage *xymsg, *reply;
  34.                                                            /* Using CreatePort() with no name       */
  35.     if (xyreplyport = CreatePort(0,0))                     /* because this port need not be public. */
  36.     {
  37.         if (xymsg = (struct XYMessage *) AllocMem(sizeof(struct XYMessage), MEMF_PUBLIC | MEMF_CLEAR))
  38.         {
  39.             xymsg->xy_Msg.mn_Node.ln_Type = NT_MESSAGE;                /* make up a message,        */
  40.             xymsg->xy_Msg.mn_Length = sizeof(struct XYMessage);        /* including the reply port. */
  41.             xymsg->xy_Msg.mn_ReplyPort = xyreplyport;
  42.             xymsg->xy_X = 10;                                   /* our special message information. */
  43.             xymsg->xy_Y = 20;
  44.  
  45.             printf("Sending to port1: x = %d y = %d\n", xymsg->xy_X, xymsg->xy_Y);
  46.                                                                    /* port2 will simply try to put  */
  47.  
  48.             if (SafePutToPort((struct Message *)xymsg, "xyport"))  /* one message to port1 wait for */
  49.             {                                                      /*  the reply, and then exit     */
  50.                 WaitPort(xyreplyport);
  51.                 if (reply = (struct XYMessage *)GetMsg(xyreplyport))
  52.                     printf("Reply contains: x = %d y = %d\n",         /* We don't ReplyMsg since   */
  53.                             xymsg->xy_X, xymsg->xy_Y);                /* WE initiated the message. */
  54.  
  55.                       /* Since we only use this private port for receiving replies, and we sent     */
  56.                       /* only one and got one reply there is no need to cleanup. For a public port, */
  57.                       /* or if you pass a pointer to the port to another process, it is a very good */
  58.                       /* habit to always handle all messages at the port before you delete it.      */
  59.             }
  60.             else printf("Can't find 'xyport'; start port1 in a separate shell\n");
  61.             FreeMem(xymsg, sizeof(struct XYMessage));
  62.         }
  63.         else printf("Couldn't get memory\n");
  64.         DeletePort(xyreplyport);
  65.     }
  66.     else printf("Couldn't create xyreplyport\n");
  67. }
  68.  
  69.  
  70. BOOL SafePutToPort(struct Message *message, STRPTR portname)
  71. {
  72.     struct MsgPort *port;
  73.  
  74.     Forbid();
  75.     port = FindPort(portname);
  76.     if (port) PutMsg(port, message);
  77.     Permit();
  78.     return(port ? TRUE : FALSE); /* FALSE if the port was not found */
  79.  
  80.          /* Once we've done a Permit(), the port might go away and leave us with an invalid port    */
  81. }        /* address. So we return just a BOOL to indicate whether the message has been sent or not. */
  82.